<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     modifier
 * Name:     numeric_format
 * Purpose:  numeric format with configurable thousands and decimal separators
 * -------------------------------------------------------------
 */
function smarty_modifier_numeric_format($value, $precision = "0", $thousand_delim = ",", $decimal_delim = ".") {
	if(!$value) return null;
	$value 		= sprintf("%.$precision"."f", $value);
	$value 		= str_replace(".", $decimal_delim, $value);
	$pos 		= strpos($value, $decimal_delim);
	if (!$pos)
		$pos 	= strlen($value);
	for ($i = $pos -3; $i > 0; $i -= 3) {
		$value	= substr($value, 0, $i).$thousand_delim.substr($value, $i);
	}

	$value 	= str_replace("-", "(", $value);
	if(substr($value, 0, 1) == "(") {
		$value 	= $value.")";
		if (substr($value, 1, 1) == $thousand_delim)
			$value 	= substr_replace($value, '', 1, 1);
	}
	return $value;
}
?>